/**
 * Created by Jaye on 2018/6/7.
 */

$(document).ready(function () {

    $('#loginBtn').click(function (e) {
        e.preventDefault();
        var l = Ladda.create(this);
        l.start();
        l.setProgress(0 - 1);
        login(l)
    });
});
function login(ladda) {
    var username = $('#username').val();
    var password = $('#password').val();
    if (checkInfo(username, password)) {
        var url = "user/login";
        var post = {
            "username": username,
            "password": password
        };
        ajax(url, post, function (data) {
            if (data.code == 1000) {
                window.location.href = "/index";
            } else {
                swal({
                    title: data.msg,
                    type: "error"
                });
            }
            ladda.stop();
        });
    } else {
        swal({
            title: "信息有误，请检查",
            type: "error"
        });
        ladda.stop();
    }
}
function checkInfo(username, password) {
    if (null != username && "" != username) {
        if (null != password && "" != password) {
            return true;
        }
    }
    return false;
}
function ajax(url, reqData, Func) {
    $.ajax(
        {
            url: url,
            data: reqData,
            type: "post",
            // async: false,
            dataType: "json",
            // contentType: "application/json;charset=UTF-8",
            beforeSend: function () {
            },
            success: function (data) {
                Func(data);
            },
            error: function () {
                swal({
                    title: "请求出错！",
                    type: "error"
                });
            }
        });
}

